const router = require('koa-router')()
const userService = require('../controllers/mySqlControll.js')
const jwt = require('../untils/jwt.js')
const { getDetailedDateTime } = require('../untils/timeUtil.js')


// 获取个人的所有的收获地址
router.get('/getCustomersAddress', async (ctx, next) => {
  const { user_id } = ctx.request.query
  try {
    const result = await userService.getCustomersAddress(user_id)
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: result,
        msg: '查询失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 删除个人的某个地址
router.post('/deleteCustomersAddress', async (ctx, next) => {
  const { id } = ctx.request.body
  // console.log(id);
  try {
    const results = await userService.deleteCustomersAddress(id);
    // console.log(results);
    console.log(results.affectedRows);
    if (results.affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: '',
        msg: '删除成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '删除失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 添加个人的某个地址
router.post('/addCustomersAddress', async (ctx, next) => {
  const { name, user_id, address, phone_number } = ctx.request.body
  // console.log(name, user_id, address, phone_number);
  try {
    if (name == '' || address == '' || phone_number == '') {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: '',
        msg: '输入框不能为空'
      }
    } else {
      const result = await userService.addCustomersAddress([name, user_id, address, phone_number])
      // console.log(result);
      if (result.affectedRows !== 0) {
        ctx.body = {
          msgType: '8000',
          code: 0,
          data: '',
          msg: '添加成功'
        }
      } else {
        ctx.body = {
          msgType: '8002',
          code: 0,
          msg: '添加失败'
        }
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})

// 添加地址到个人的地址
router.post('/addCustomersAddressInUser', async (ctx, next) => {
  const { user_id,address_id} = ctx.request.body
  // console.log(user_id,address_id);
  try {
    const result = await userService.addCustomersAddressInUser(user_id,address_id)
    if (result.affectedRows !== 0) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        msg: '使用成功'
      } 
    }else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        msg: '使用失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 0,
      data: error,
      msg: '服务器异常'
    }
  }
})


// 获取用户的地址通过address_id
router.post('/getShoppingAddressFromId', async (ctx, next) => {
  const { address_id } = ctx.request.body
  // console.log(address_id);
  try {
    const result = await userService.getShoppingAddressFromId(address_id )
    if (result) {
      ctx.body = {
        msgType: '8000',
        code: 0,
        data: result,
        msg: '查询成功'
      }
    } else {
      ctx.body = {
        msgType: '8002',
        code: 0,
        data: result,
        msg: '查询失败'
      }
    }
  } catch (error) {
    ctx.body = {
      msgType: '8002',
      code: 1,
      data: error,
      msg: '服务器异常'
    }
  }
})






module.exports = router